﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Vds2465Telegrams
{
    [TestClass]
    public class UserData_24_BlockState_Test
    {
        [TestMethod]
        [Owner("Frank Eppink")]
        public void Test01_Equals()
        {
            byte octet01 = BlockStateByte_24_26.CreateOctet01(true, true, true, true);
            byte octet02 = BlockStateByte_24_26.CreateOctet02(false);
            byte octet03 = BlockStateByte_24_26.CreateOctet03(true, false);
            UserData_24_BlockState userData = new UserData_24_BlockState(0x67, 0x23, 0x54, eAddressExtension_1.Disruption, new byte[] { octet01, octet02, octet03 });

            {
                // completely different type - NOT equal
                object dummyObject = new object();
                Assert.IsFalse(userData.Equals(dummyObject));
            }

            {
                // different derived type 
                UserData_11_Error userData2Compare = new UserData_11_Error(eErrorCode.DataOutOfRange, 0x99);
                Assert.IsFalse(userData.Equals(userData2Compare));
            }

            {
                // same object - equal
                UserData_24_BlockState userData2Compare = new UserData_24_BlockState(userData.Device, userData.Address, userData.AddressAddOn, userData.AddressExtension,
                            userData.GetCopyOfBlockStateBytes());
                Assert.IsTrue(userData.Equals(userData2Compare));
            }

            {
                // different device
                UserData_24_BlockState userData2Compare = new UserData_24_BlockState((byte)(userData.Device + 1), userData.Address, userData.AddressAddOn,
                            userData.AddressExtension, userData.GetCopyOfBlockStateBytes());
                Assert.IsFalse(userData.Equals(userData2Compare));
            }

            {
                // different address
                UserData_24_BlockState userData2Compare = new UserData_24_BlockState(userData.Device, (byte)(userData.Address + 1), userData.AddressAddOn,
                            userData.AddressExtension, userData.GetCopyOfBlockStateBytes());
                Assert.IsFalse(userData.Equals(userData2Compare));
            }

            {
                // different AddressAddOn
                UserData_24_BlockState userData2Compare = new UserData_24_BlockState(userData.Device, userData.Address, (byte)(userData.AddressAddOn + 1),
                            userData.AddressExtension, userData.GetCopyOfBlockStateBytes());
                Assert.IsFalse(userData.Equals(userData2Compare));
            }

            {
                // different block state bytes - one more member
                byte[] blockStateBytesCopy = userData.GetCopyOfBlockStateBytes();
                byte[] blockStateBytes2Compare = new byte[blockStateBytesCopy.Length + 1];
                for (int index = 0; index < blockStateBytesCopy.Length; index++)
                {
                    blockStateBytes2Compare[index] = blockStateBytesCopy[index];
                }
                blockStateBytes2Compare[blockStateBytesCopy.Length] = 0x55;

                UserData_24_BlockState userData2Compare = new UserData_24_BlockState(userData.Device, userData.Address, userData.AddressAddOn,
                            userData.AddressExtension, blockStateBytes2Compare);
                Assert.IsFalse(userData.Equals(userData2Compare));
            }

            {
                // different input bytes - one different member
                byte[] blockStateBytesCopy = userData.GetCopyOfBlockStateBytes();
                byte[] blockStateBytes2Compare = new byte[blockStateBytesCopy.Length];
                for (int index = 0; index < blockStateBytesCopy.Length; index++)
                {
                    blockStateBytes2Compare[index] = blockStateBytesCopy[index];
                    if (index == 0)
                    {
                        blockStateBytes2Compare[index] = (byte)(blockStateBytesCopy[index] + 1);
                    }
                }

                UserData_24_BlockState userData2Compare = new UserData_24_BlockState(userData.Device, userData.Address, userData.AddressAddOn,
                            userData.AddressExtension, blockStateBytes2Compare);
                Assert.IsFalse(userData.Equals(userData2Compare));
            }
        }

        [TestMethod]
        [Owner("Frank Eppink")]
        public void Test02_Equals_BytesAllNull()
        {
            UserData_24_BlockState userData = new UserData_24_BlockState(0x67, 0x23, 0x54, eAddressExtension_1.Input, null);

            {
                // completely different type - NOT equal
                object dummyObject = new object();
                Assert.IsFalse(userData.Equals(dummyObject));
            }

            {
                // different derived type 
                UserData_11_Error userData2Compare = new UserData_11_Error(eErrorCode.DataOutOfRange, 0x99);
                Assert.IsFalse(userData.Equals(userData2Compare));
            }

            {
                // same object - equal
                UserData_24_BlockState userData2Compare = new UserData_24_BlockState(userData.Device, userData.Address, userData.AddressAddOn, userData.AddressExtension,
                            userData.GetCopyOfBlockStateBytes());
                Assert.IsTrue(userData.Equals(userData2Compare));
            }

            {
                // different device
                UserData_24_BlockState userData2Compare = new UserData_24_BlockState((byte)(userData.Device + 1), userData.Address, userData.AddressAddOn,
                            userData.AddressExtension, userData.GetCopyOfBlockStateBytes());
                Assert.IsFalse(userData.Equals(userData2Compare));
            }

            {
                // different address
                UserData_24_BlockState userData2Compare = new UserData_24_BlockState(userData.Device, (byte)(userData.Address + 1), userData.AddressAddOn,
                            userData.AddressExtension, userData.GetCopyOfBlockStateBytes());
                Assert.IsFalse(userData.Equals(userData2Compare));
            }

            {
                // different AddressAddOn
                UserData_24_BlockState userData2Compare = new UserData_24_BlockState(userData.Device, userData.Address, (byte)(userData.AddressAddOn + 1),
                            userData.AddressExtension, userData.GetCopyOfBlockStateBytes());
                Assert.IsFalse(userData.Equals(userData2Compare));
            }

            {
                // different block state bytes - one more member
                byte[] blockStateBytesCopy = userData.GetCopyOfBlockStateBytes();
                byte[] blockStateBytes2Compare = new byte[blockStateBytesCopy.Length + 1];
                for (int index = 0; index < blockStateBytesCopy.Length; index++)
                {
                    blockStateBytes2Compare[index] = blockStateBytesCopy[index];
                }
                blockStateBytes2Compare[blockStateBytesCopy.Length] = 0x55;

                UserData_24_BlockState userData2Compare = new UserData_24_BlockState(userData.Device, userData.Address, userData.AddressAddOn,
                            userData.AddressExtension, blockStateBytes2Compare);
                Assert.IsFalse(userData.Equals(userData2Compare));
            }           
        }

        [TestMethod]
        [Owner("Frank Eppink")]
        public void Test03_GetHashCode()
        {
            {
                UserData_24_BlockState userData = new UserData_24_BlockState(0x67, 0x23, 0x54, eAddressExtension_1.Input, new byte[] { 0xff, 0xaa, 0x55 });
                int hashCode = userData.GetHashCode();
            }

            {
                UserData_24_BlockState userData = new UserData_24_BlockState(0x67, 0x23, 0x54, eAddressExtension_1.Input, null);
                int hashCode = userData.GetHashCode();
            }

            {
                UserData_24_BlockState userData = new UserData_24_BlockState(0x67, 0x23, 0x54, eAddressExtension_1.Input, new byte[0]);
                int hashCode = userData.GetHashCode();
            }
        }

        [TestMethod]
        [Owner("Frank Eppink")]
        public void Test04_ToString()
        {
            {
                UserData_24_BlockState userData = new UserData_24_BlockState(0x67, 0x23, 0x54, eAddressExtension_1.Input, new byte[] { 0xff, 0xaa, 0x55 });
                string stringResult = userData.ToString();
                Assert.IsNotNull(stringResult);
            }

            {
                UserData_24_BlockState userData = new UserData_24_BlockState(0x67, 0x23, 0x54, eAddressExtension_1.Input, null);
                string stringResult = userData.ToString();
                Assert.IsNotNull(stringResult);
            }

            {
                UserData_24_BlockState userData = new UserData_24_BlockState(0x67, 0x23, 0x54, eAddressExtension_1.Input, new byte[0]);
                string stringResult = userData.ToString();
                Assert.IsNotNull(stringResult);
            }
        }
    }
}
